package edu.cdnu.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import edu.cdnu.dao.CommonDao;
import edu.cdnu.pojo.DictionaryPojo;
import edu.cdnu.service.CommonService;

@Service
public class CommonServiceImpl implements CommonService {

	@Autowired
	private CommonDao commonDao;
	
	@Override
	public List<DictionaryPojo> selectAllDepartmentList() {
		return commonDao.selectAllDepartmentList();
	}

	@Override
	public List<DictionaryPojo> selectDictionaryListByDecription(String decription) {
		// 不为空才查询
		if(decription != null && !"".equals(decription)) {
			return commonDao.selectDictionaryListByDecription(decription);
		}
		return null;
	}

	@Override
	public List<DictionaryPojo> selectDictionaryListByParentId(Integer parentId) {
		// 不为空才查询
		if(parentId !=null) {
			return commonDao.selectDictionaryListByParentId(parentId);
		}
		return null;
	}

	// 这个方法里面全用的ObjectUtils工具来判空；也能判断空字符等，比自己写的好用
	@Override
	public DictionaryPojo selectDictionary(DictionaryPojo dictionary) { 
		// 判空
		if(ObjectUtils.isEmpty(dictionary)) {
			return null;
		}
		QueryWrapper<DictionaryPojo> queryWrapper=new QueryWrapper<>();
		// id
		if(!ObjectUtils.isEmpty(dictionary.getId())) {
			queryWrapper.eq("id", dictionary.getId());
		}
		// name
		if(!ObjectUtils.isEmpty(dictionary.getName())) {
			queryWrapper.eq("name", dictionary.getName());
		}
		// parent_id
		if(!ObjectUtils.isEmpty(dictionary.getParentId())) {
			queryWrapper.eq("parent_id", dictionary.getParentId());
		}
		// decription
		if(!ObjectUtils.isEmpty(dictionary.getDecription())) {
			queryWrapper.eq("decription", dictionary.getDecription());
		}
		
		return commonDao.selectOne(queryWrapper);
	}


	
}
